import urllib.request
import random

# 我的ip地址查询
# url = 'http://www.whatismyip.com.tw'
url = 'http://httpbin.org/get'

# ip随机使用列表
iplist = ['49.81.125.43:9000', '183.230.177.24:8060', '']

proxy = '117.191.11.105:8080'

# 1.参数为字典{'类型':'代理ip:端口号'}
# proxy_support = urllib.request.ProxyHandler({'https': '192.168.11.210'})
proxy_support = urllib.request.ProxyHandler({'http': proxy})
# proxy_support = urllib.request.ProxyHandler({'http': random.choice(iplist)})

# 2.定制，创建一个opener
opener = urllib.request.build_opener(proxy_support)
# 添加请求头
opener.addheaders = [('User-Agent',
                      'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36')]

# 3a.安装opener
urllib.request.install_opener(opener)

# 3b.调用opener
# response = opener.open(url)

req = urllib.request.Request(url)
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')
print(html)
